//2009/09/09 11:06:12
#include <string>
#include <vector>
#include <map>
#include <queue>
#include <sstream>
#include <algorithm>

using namespace std;

class WordWrap
{
public:
    vector <string> justify(vector <string> lines, int cw)
    {
		vector<string> ret;
		string s;
		for(int i=0; i<lines.size(); i++)
		{
			stringstream stm(lines[i]);
			string s1;
			while(stm >> s1)
			{
				if(s.size() + s1.size() <= cw)
				{
					s += " ";
					s += s1;
				}
				else
				{
					ret.push_back(s.substr(1));
					s.clear();
					s += " ";
					s += s1;
				}
			}
		}
		ret.push_back(s.substr(1));
		return ret;
    }
};
